package com.example.administrator.familfinalcialmanagement.ui.fragment;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.administrator.familfinalcialmanagement.R;
import com.example.administrator.familfinalcialmanagement.bean.GreenDaoUtils;
import com.example.administrator.familfinalcialmanagement.bean.PayInfoBean;
import com.example.administrator.familfinalcialmanagement.bean.WalletBean;
import com.example.administrator.familfinalcialmanagement.databinding.FragmentSecondBinding;
import com.example.administrator.familfinalcialmanagement.greendao.gen.PayInfoBeanDao;
import com.example.administrator.familfinalcialmanagement.greendao.gen.WalletBeanDao;
import com.example.administrator.familfinalcialmanagement.util.LogUtil;
import com.example.administrator.familfinalcialmanagement.util.TimeUtil;
import com.example.administrator.familfinalcialmanagement.util.ToastUtil;
import com.example.administrator.familfinalcialmanagement.util.dialog.PayDialog;
import com.example.administrator.familfinalcialmanagement.util.listener.OnGRClickListener;

import java.util.List;

/**
 * Created by Administrator on 2019/3/27.
 */

public class SecondFragment extends BaseLazyFragment {

    FragmentSecondBinding binding;
    String timeNow;
    PayInfoBeanDao daobeanDao = GreenDaoUtils.getSingleTon().getmDaoSession().getPayInfoBeanDao();
    //1 支出   2 收入
    String payStatus = "收入",payStyle = "0";
    WalletBeanDao walletBeanDao = GreenDaoUtils.getSingleTon().getmDaoSession().getWalletBeanDao();

    @Override
    protected View inflaterView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(LayoutInflater.from(getActivity()), R.layout.fragment_second, container, false);
        binding.setFragment(this);
        return binding.getRoot();
    }

    @Override
    protected void onVisible() {

    }

    @Override
    protected void lazyLoad() {

        binding.tvModuleMan.setOnClickListener(this);
        binding.tvModuleWoman.setOnClickListener(this);
        binding.tvModuleUp.setOnClickListener(this);
        binding.llModulePayStyle.setOnClickListener(this);
    }

    @Override
    public int setLayoutId() {
        return R.layout.fragment_second;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tvModuleMan:
                binding.tvModuleMan.setBackground(getResources().getDrawable(R.drawable.draw_activity_myinfo_sex_man));
                binding.tvModuleWoman.setBackground(getResources().getDrawable(R.drawable.draw_activity_myinfo_sex_woman));
                payStatus = "收入";
                break;
            case R.id.tvModuleWoman:
                binding.tvModuleMan.setBackground(getResources().getDrawable(R.drawable.draw_activity_myinfo_sex_man_noselect));
                binding.tvModuleWoman.setBackground(getResources().getDrawable(R.drawable.draw_activity_myinfo_sex_woman_select));
                payStatus = "支出";
                break;
            case R.id.llModulePayStyle:
                PayDialog dialog = new PayDialog(getActivity());
                dialog.show();
                dialog.setOnGRClickListener(new OnGRClickListener() {
                    @Override
                    public void setOnConfirmClick(String res) {
                        binding.tvModulePayStyle.setText(res);
                    }

                    @Override
                    public void setOnCloseClick() {

                    }
                });
                break;
            case R.id.tvModuleUp:

                String title = binding.etModuleTitle.getText().toString().trim();
                if (TextUtils.isEmpty(title)) {
                    ToastUtil.showLongToast("请输入账目标题");
                    return;
                }

                String info = binding.etModuleInfo.getText().toString().trim();
                if (TextUtils.isEmpty(info)) {
                    ToastUtil.showLongToast("请输入备注说明");
                    return;
                }

                if ("0".equals(payStatus)) {
                    ToastUtil.showLongToast("请选择账目类型（收入/支出)");
                    return;
                }

                payStyle = binding.tvModulePayStyle.getText().toString().trim();
                if ("0".equals(payStyle) || TextUtils.isEmpty(payStyle)) {
                    ToastUtil.showToast("请选择支付类型");
                    return;
                }

                String money = binding.etModuleMoney.getText().toString().trim();
                if (TextUtils.isEmpty(money)) {
                    ToastUtil.showToast("请输入金额");
                    return;
                }

                timeNow = TimeUtil.getNowTime();

                //获取消费的类型,比如是支付宝还是微信
                List<WalletBean> list = walletBeanDao.loadAll();
                if (list.size() == 0) {
                    ToastUtil.showToast("请先添加您的钱包");
                    return;
                }

                PayInfoBean bean = new PayInfoBean();
                bean.setId(TimeUtil.getNowTimeLong());
                bean.setInfo(info);
                bean.setPayStatus(payStatus);
                bean.setPayStyle(payStyle);
                bean.setTitle(title);
                bean.setPayTime(timeNow);
                bean.setMoney(money);
                daobeanDao.insert(bean);

                for(WalletBean bean1 : list){
                    if (bean1.getWalletName().equals(payStyle)) {
                        LogUtil.e("消费金额-->"+ money +
                                "\n消费类型-->" + payStyle +
                                "\n钱包消费前剩余-->" + bean1.getMoney() +
                                "\n消费后剩余-->"+ (Double.valueOf(money) - Double.valueOf(bean1.getMoney())));
                        Double residue =  Double.valueOf(bean1.getMoney()) - Double.valueOf(money);
                        bean1.setMoney(residue + "");
                        walletBeanDao.update(bean1);
                        LogUtil.e("保存前数据库查询余额"+ bean1.getMoney());
                    }
                }

                ToastUtil.showToast("保存完毕");
                binding.etModuleInfo.setText("");
                binding.etModuleTitle.setText("");
                binding.etModuleMoney.setText("");
                break;
            default:
        }
    }
}